﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using RepertorySystem.Models;

namespace RepertorySystem.Controllers
{
    [Authorize]
    public class BookModelsController : Controller
    {
        private BookDbContext db = new BookDbContext();

        // GET: BookModels
        public ActionResult Index()
        {
            int arryLength = db.Books.Count();
            int totalPages;
            if (arryLength % 5 != 0) {
                totalPages = (arryLength / 5) + 1;
            } else {
                totalPages = (arryLength / 5);
            }

            List<BookModels> content = db.Books.OrderByDescending(u => u.ID).Skip(0).Take(5).ToList();
            ViewBag.content = content;
            ViewBag.totalPages = totalPages;
            ViewBag.currentPage = 1;

            return View();
        }


        public ActionResult GetNext(int currentPage) {
            int arryLength = db.Books.Count();
            int totalPages;
            if (arryLength % 5 != 0) {
                totalPages = (arryLength / 5) + 1;
            } else {
                totalPages = (arryLength / 5);
            }

            List<BookModels> content = db.Books.OrderByDescending(u => u.ID).Skip((currentPage - 1) * 5).Take(5).ToList();
            ViewBag.content = content;
            ViewBag.currentPage = currentPage;
            ViewBag.totalPages = totalPages;
            return View("Index");

        }

        public ActionResult FindOneAccountList() {
            String book_Name = this.TempData["book_Name"] as String;

            BookModels book = db.Books.Where(u => u.Name == book_Name).FirstOrDefault();

            if(book == null) {
                return RedirectToAction("ReturnNoFind", "Accountlists", "");
            }

            this.TempData["resultBook"] = book;

            return RedirectToAction("ReturnOne", "Accountlists", "");
        }

        public ActionResult FindOneOutputStorage() {
            String book_Name = this.TempData["book_Name"] as String;

            BookModels book = db.Books.Where(u => u.Name == book_Name).FirstOrDefault();

            if (book == null) {
                return RedirectToAction("ReturnNoFind", "OutputStorages", "");
            }

            this.TempData["resultBook"] = book;

            return RedirectToAction("ReturnOne", "OutputStorages", "");
        }

        public ActionResult FindOneInputStorage() {
            String book_Name = this.TempData["book_Name"] as String;

            BookModels book = db.Books.Where(u => u.Name == book_Name).FirstOrDefault();

            if (book == null) {
                return RedirectToAction("ReturnNoFind", "InputStorages", "");
            }

            this.TempData["resultBook"] = book;

            return RedirectToAction("ReturnOne", "InputStorages", "");
        }

        public ActionResult FindOneInsertOrderLists() {
            String book_Name = this.TempData["book_Name"] as String;

            BookModels book = db.Books.Where(u => u.Name == book_Name).FirstOrDefault();

            if (book == null) {
                return RedirectToAction("ReturnNoFind", "InsertOrderLists", "");
            }

            this.TempData["resultBook"] = book;

            return RedirectToAction("ReturnOne", "InsertOrderLists", "");
        }

        public ActionResult FindOneOrderLists() {
            String book_Name = this.TempData["book_Name"] as String;

            BookModels book = db.Books.Where(u => u.Name == book_Name).FirstOrDefault();

            if (book == null) {
                return RedirectToAction("ReturnNoFind", "OrderLists", "");
            }

            this.TempData["resultBook"] = book;

            return RedirectToAction("ReturnOne", "OrderLists", "");
        }

        public ActionResult FindStorageLists() {
            String book_Name = this.TempData["book_Name"] as String;

            BookModels book = db.Books.Where(u => u.Name == book_Name).FirstOrDefault();

            if (book == null) {
                return RedirectToAction("ReturnNoFind", "StorageLists", "");
            }

            this.TempData["resultBook"] = book;

            return RedirectToAction("ReturnOne", "StorageLists", "");
        }

        public ActionResult FindBookModels() {
            String book_Name = this.TempData["book_Name"] as String;

            BookModels book = db.Books.Where(u => u.Name == book_Name).FirstOrDefault();

            if (book == null) {
                return RedirectToAction("ReturnNoFind", "BookModels", "");
            }

            this.TempData["resultBook"] = book;

            return RedirectToAction("ReturnOne", "BookModels", "");
        }


        public ActionResult FindOne(String book_Name) {
            //查询数据
            this.TempData["book_Name"] = book_Name;
            //跳转
            return RedirectToAction("FindBookModels", "BookModels", "");

        }

        public ActionResult ReturnOne() {
            BookModels book = new BookModels();
            book = this.TempData["resultBook"] as BookModels;
            int id = book.ID;

            List<BookModels> accountList = db.Books.Where(a => a.ID == book.ID).ToList();


            ViewBag.content = accountList;
            ViewBag.currentPage = 1;
            ViewBag.totalPages = 1;


            return View("Index");
        }

        public ActionResult ReturnNoFind() {

            ViewBag.content = null;
            ViewBag.currentPage = 0;
            ViewBag.totalPages = 0;
            return View("Index");
        }


        // GET: BookModels/Details/5
        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            BookModels bookModels = db.Books.Find(id);
            if (bookModels == null)
            {
                return HttpNotFound();
            }
            return View(bookModels);
        }

        // GET: BookModels/Create
        public ActionResult Create()
        {
            return View();
        }

        // POST: BookModels/Create
        // 为了防止“过多发布”攻击，请启用要绑定到的特定属性，有关 
        // 详细信息，请参阅 http://go.microsoft.com/fwlink/?LinkId=317598。
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = "ID,Name,Type,InPrice,OutPrice")] BookModels bookModels)
        {
            if (ModelState.IsValid)
            {
                db.Books.Add(bookModels);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(bookModels);
        }

        // GET: BookModels/Edit/5
        public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            BookModels bookModels = db.Books.Find(id);
            if (bookModels == null)
            {
                return HttpNotFound();
            }
            return View(bookModels);
        }

        // POST: BookModels/Edit/5
        // 为了防止“过多发布”攻击，请启用要绑定到的特定属性，有关 
        // 详细信息，请参阅 http://go.microsoft.com/fwlink/?LinkId=317598。
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include = "ID,Name,Type,InPrice,OutPrice")] BookModels bookModels)
        {
            if (ModelState.IsValid)
            {
                db.Entry(bookModels).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(bookModels);
        }

        // GET: BookModels/Delete/5
        public ActionResult Delete(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            BookModels bookModels = db.Books.Find(id);
            if (bookModels == null)
            {
                return HttpNotFound();
            }
            return View(bookModels);
        }

        // POST: BookModels/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            BookModels bookModels = db.Books.Find(id);
            db.Books.Remove(bookModels);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}
